﻿using FieldTools.Client.Business.IBusiness;
using FieldTools.Client.Entity.Model;
using FieldTools.Client.Model.Window;
using FieldTools.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FieldTools.Client.Business.Business
{
    /// <summary>
    /// 自定义接口，动态api
    /// </summary>
    public class CustomApiBus : BaseBus, ICustomApiBus, ITransientDependency
    {
        private HttpServerModel httpServerModel => CacheHelper.GetOrSetCache(nameof(HttpServerModel), new HttpServerModel());
        /// <inheritdoc/>
        public string? Get(string path)
        {
            if (string.IsNullOrEmpty(path)) return "路由路径以“api/custom/”开头且后续不可为空";
            var model = httpServerModel;
            try
            {
                var api = model.CustomApis.First(item => item.IsEnable && item.RequestType == RequestType.GET && !string.IsNullOrEmpty(item.Path) && item.Path.Equals(path));
                return api?.Excute();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// <inheritdoc/>
        public string? Post(string path, object parm)
        {
            if (string.IsNullOrEmpty(path)) return "路由路径以“api/custom/”开头且后续不可为空";
            var model = httpServerModel;
            try
            {
                var api = model.CustomApis.First(item => item.IsEnable && item.RequestType == RequestType.POST && !string.IsNullOrEmpty(item.Path) && item.Path.Equals(path));
                if(api!=null) api.Parm = parm;
                return api?.Excute();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
